package com.example.shangjinlieren.slice;

import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.tabbar.entity.BottomNavItem;
import com.example.shangjinlieren.tabbar.fraction.FractionUtil;
import com.example.shangjinlieren.tabbar.model.BottomNavViewModel;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

//    底部导航所需变量 start
    private Integer currentSaveIndex=0;//当前正在显示的索引值
    private List<BottomNavItem> bottomNavItems;
    private Context context;
    private FractionUtil fractionUtil;
    List<Fraction> fractions;
    BottomNavViewModel bottomNavViewModel;
//    底部导航所需变量 end
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //    底部导航所需变量 start
        this.bottomNavViewModel = new BottomNavViewModel();
        this.fractions =  bottomNavViewModel.initBottomNavFractionsBody();
        this.bottomNavItems=bottomNavViewModel.initBottomNavFractions();
        fractionUtil=new FractionUtil(fractions,((FractionAbility)getAbility()),ResourceTable.Id_main_content);
        initBottom();
        //    底部导航所需变量 end
    }

    @Override
    public void onActive() {
        super.onActive();
    }
//    底部导航所需变量 start
    public void  initBottom(){
        //创建底部容器数据数组
        List<DirectionalLayout> tabList = new ArrayList<>();
        //获取主页面底部容器ID

        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        //顶部导航 nav_top_bar_title
        DirectionalLayout NavTopBaCompontentr =(DirectionalLayout) findComponentById(ResourceTable.Id_nav_top_bar);
        DependentLayout parse =(DependentLayout) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_nav_top_bar, null, false);
        Text navTopBarTitle = (Text) parse.findComponentById(ResourceTable.Id_nav_top_bar_title);
        navTopBarTitle.setText(this.bottomNavItems.get(this.currentSaveIndex).getTitle());
        NavTopBaCompontentr.addComponent(parse);
        for (int i = 0; i <this.bottomNavItems.size() ; i++) {
            //获取模板容器
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())

                    .parse(ResourceTable.Layout_bottom_nav_tab, bottomTab, false);
            tabList.add(tab);
            //获取模板容器组件
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            if (buttonImage != null) {//
                if (i == this.currentSaveIndex) {
                    buttonImage.setPixelMap(this.bottomNavItems.get(i).getSelectImg());
                } else {
                    buttonImage.setPixelMap(this.bottomNavItems.get(i).getImg());
                }
            }
            if (buttonText != null) {
                buttonText.setText(this.bottomNavItems.get(i).getTitle());
                if (i == this.currentSaveIndex) {
                    int rgb = Color.getIntColor(this.bottomNavItems.get(i).getSelectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                } else {
                    int rgb = Color.getIntColor(this.bottomNavItems.get(i).getUnselectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                }

            }
            //绑定点击事件
            bottomNavBarClick(tab,i,tabList);
            bottomTab.addComponent(tab);
        }
    }
    public void bottomNavBarClick(DirectionalLayout item, int index,List<DirectionalLayout> tabList){


        item.setClickedListener(component -> {
            //获取模板容器组件
            Text navTopBarTitle = (Text) findComponentById(ResourceTable.Id_nav_top_bar_title);

            for (int i = 0; i <this.bottomNavItems.size() ; i++) {
                Image buttonImage = (Image) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_img);
                Text buttonText = (Text) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_text);
                if (buttonImage != null&&buttonText != null) {//
                    if (i == index) {
                        System.out.println("当前索引"+index);
                        navTopBarTitle.setText(this.bottomNavItems.get(index).getTitle());
                        buttonImage.setPixelMap(this.bottomNavItems.get(i).getSelectImg());
                        int rgb = Color.getIntColor(this.bottomNavItems.get(i).getSelectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    } else {
                        buttonImage.setPixelMap(this.bottomNavItems.get(i).getImg());
                        int rgb = Color.getIntColor(this.bottomNavItems.get(i).getUnselectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    }
                }
            }
            fractionUtil.show(index);
        });
    }
// 底部导航所需变量 end
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
